package com.yuandian.business.monitor.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuandian.api.business_monitor.po.CockpitParams;
import com.yuandian.api.business_monitor.vo.*;
import com.yuandian.api.management.entity.ConfSegment;
import com.yuandian.api.management.entity.ConfSystem;
import com.yuandian.api.management.feign.RemoteBusinessService;
import com.yuandian.api.management.feign.RemoteSegmentService;
import com.yuandian.api.management.feign.RemoteSystemService;
import com.yuandian.bpm.common.core.constant.CommonConstants;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.business.monitor.mapper.CockpitMapper;
import com.yuandian.business.monitor.service.IAppSystemService;
import com.yuandian.business.monitor.service.ICockpitService;
import com.yuandian.constant.Constants;
import com.yuandian.enums.ScaleTypeEnum;
import com.yuandian.exception.SqlTemplateException;
import com.yuandian.po.BaseSearch;
import com.yuandian.utils.PartitionUtil;
import com.yuandian.utils.StringUtils;
import com.yuandian.utils.YDateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.stream.Collectors;

/**
 * 说明：首页大屏数据驾驶舱service实现
 *
 * @Author yanjun
 * @Date 2020/9/25 16:27
 */
@Service
@Slf4j
public class CockpitServiceImpl implements ICockpitService {

	@Autowired
	private CockpitMapper cockpitMapper;


	@Autowired
	private RemoteSystemService systemService;
	@Autowired
	private RemoteBusinessService remoteBusinessService;
	@Autowired
	private RemoteSegmentService remoteSegmentService;
	@Autowired
	private IAppSystemService iAppSystemService;

//    @Autowired
//    private ISynchronousBusinessService synchronousBusinessService;
//
//    @Autowired
//    private ISegmentService segmentService;

	/**
	 * 查询系统总览 之前展示默认前五个系统，新需求改为展示请求量前五的系统
	 *
	 * @param params 首页大屏数据驾驶舱搜索条件
	 * @return 首页大屏-系统总览vo对象
	 */
	@Override
	public List<SystemOverview> systemOverview(CockpitParams params) {
		// 1、查询系统总览：找请求量前五
		List<SystemOverview> systemOverviewList = cockpitMapper.systemOverview(params);
		List<Integer> systemIdList = params.getSystemIdList();
		//2、如果请求量不到五个系统且前端没有勾选则取默认前五个补齐
		if (ObjectUtil.isEmpty(systemIdList) && (ObjectUtil.isEmpty(systemOverviewList) || (ObjectUtil.isNotEmpty(systemOverviewList) && systemOverviewList.size() < 5))) {

			R r = iAppSystemService.queryKeySystemTop5();
			if (r.getCode() == CommonConstants.FAIL) {
				log.error("调用远程服务获取前五系统异常，错误信息：" + r.getMsg());
				throw new SqlTemplateException(r.getMsg());
			}
			Page<ConfSystem> page = (Page<ConfSystem>) r.getData();
			List<ConfSystem> records = page.getRecords();
			params.setSystemIdList(records.stream().map(ConfSystem::getId).collect(Collectors.toList()));
			if (ObjectUtil.isNotEmpty(records)) {
				for (ConfSystem record : records) {
					SystemOverview systemOverview = new SystemOverview();
					systemOverview.setSystemName(record.getName());
					systemOverview.setSystemId(record.getId());
					if (!systemOverviewList.contains(systemOverview)) {
						systemOverviewList.add(systemOverview);
					}
					if (systemOverviewList.size() == 5) {
						break;
					}
				}
			}

		}

		//3、如果查询到的请求量系统数小于勾选系统数，则需要补齐
		boolean complementFlag = ObjectUtil.isEmpty(systemOverviewList) || systemOverviewList.size() < systemIdList.size();
		List<Integer> resultSystemIds = systemOverviewList.stream().map(SystemOverview::getSystemId).collect(Collectors.toList());
		//4、查询系统业务数
		R<List<Map<String, Integer>>> businessCountBySystemIdsR = remoteBusinessService.getBusinessCountBySystemIds(complementFlag ? systemIdList : resultSystemIds);
		if (businessCountBySystemIdsR.getCode() == CommonConstants.FAIL) {
			log.error("调用远程服务获取每个系统对应的业务数失败，错误信息：" + businessCountBySystemIdsR.getMsg());
			throw new SqlTemplateException(businessCountBySystemIdsR.getMsg());
		}
		log.info("调用远程服务获取每个系统对应的业务数结束！");
		List<Map<String, Integer>> businessCountList = businessCountBySystemIdsR.getData();
		//3、如果指定5个系统，数据不够需要手动补全
		if (complementFlag) {
			for (Integer systemId : systemIdList) {
				if (!resultSystemIds.contains(systemId)) {
					SystemOverview systemOverview = new SystemOverview();
					systemOverview.setSystemId(systemId);
					systemOverview.setSystemName(systemService.getSystemById(systemId).getData().getName());
					systemOverviewList.add(systemOverview);
				}
			}
		}
		for (SystemOverview systemOverview : systemOverviewList) {
			Integer systemId = systemOverview.getSystemId();
			for (Map<String, Integer> businessCountMap : businessCountList) {
				Integer systemIdTemp = businessCountMap.get("systemId");
				if (systemId.equals(systemIdTemp)) {
					systemOverview.setSystemBusiTotal(businessCountMap.get("businessCount"));
				}
			}

		}
		return systemOverviewList;
	}

	/**
	 * 查询失败量趋势
	 *
	 * @param params 首页大屏数据驾驶舱搜索条件
	 * @return 首页大屏-失败量趋势vo对象
	 */
	@Override
	public FailTrendVo failTrend(CockpitParams params) {
		FailTrendVo failTrendVo = new FailTrendVo();
		List<FailTrend> dayList = new ArrayList<>();
		List<FailTrend> weekList = new ArrayList<>();
		List<FailTrend> monthList = new ArrayList<>();

		String startTimeStr = params.getStartTime();
		String endTimeStr = params.getEndTime();
		String endTime = YDateUtil.format(YDateUtil.parse(endTimeStr, YDateUtil.YYYY_MM_DD_HH_MM_SS), YDateUtil.HH_MM_SS);
		if (!"00:00:00".equals(endTime)) {
			params.setEndTimeStr(YDateUtil.format(YDateUtil.parse(endTimeStr, YDateUtil.YYYY_MM_DD_HH_MM_SS), YDateUtil.HH_MM_SS));    // 结束时间字符串
		}


		// 1、查询今日访问失败量
		List<FailTrend> failTrendDay = cockpitMapper.failTrend(params, 1);

		// 2、查询过去七天访问失败量
		String weekStartTime = YDateUtil.getBeforeDayTime(startTimeStr, 7);
		String weekEndTime = YDateUtil.getBeforeDayTime(endTimeStr, 1);
		params.setStartTime(weekStartTime);
		params.setEndTime(weekEndTime);
		List<FailTrend> failTrendWeek = cockpitMapper.failTrend(params, 7);

		// 3、查询过去一个月访问失败量
		String monthStartTime = YDateUtil.getBeforeDayTime(startTimeStr, 30);
		String monthEndTime = YDateUtil.getBeforeDayTime(endTimeStr, 1);
		params.setStartTime(monthStartTime);
		params.setEndTime(monthEndTime);
		List<FailTrend> failTrendMonth = cockpitMapper.failTrend(params, 30);

		// 4. 组装结果集, 进行补0操作
		List<String> timeList = YDateUtil.getTimeListByJDK8(startTimeStr, endTimeStr, YDateUtil.HH_MM, 1);
		Map<String, FailTrend> dayCollectMap = failTrendDay.stream().collect(Collectors.toMap(FailTrend::getLabelTime, v -> v, (k1, k2) -> k1));
		Map<String, FailTrend> weekCollectMap = failTrendWeek.stream().collect(Collectors.toMap(FailTrend::getLabelTime, v -> v, (k1, k2) -> k1));
		Map<String, FailTrend> monthCollectMap = failTrendMonth.stream().collect(Collectors.toMap(FailTrend::getLabelTime, v -> v, (k1, k2) -> k1));
		for (String time : timeList) {
			// 初始化对象，用来补0操作
			FailTrend supplementVo = new FailTrend();
			supplementVo.setLabelTime(time);
			supplementVo.setFailVisitCount(0);

			FailTrend dayVo = dayCollectMap.get(time);
			FailTrend weekVo = weekCollectMap.get(time);
			FailTrend monthVo = monthCollectMap.get(time);
			dayList.add(ObjectUtil.isNotNull(dayVo) ? dayVo : supplementVo);
			weekList.add(ObjectUtil.isNotNull(weekVo) ? weekVo : supplementVo);
			monthList.add(ObjectUtil.isNotNull(monthVo) ? monthVo : supplementVo);
		}

		failTrendVo.setFailTrendDay(dayList);
		failTrendVo.setFailTrendWeek(weekList);
		failTrendVo.setFailTrendMonth(monthList);

		return failTrendVo;
	}

	/**
	 * 查询系统业务访问量TopN
	 *
	 * @param params 首页大屏数据驾驶舱搜索条件
	 * @return 首页大屏-系统业务访问量TopN vo对象
	 */
	@Override
	public List<BusinessVisitsTopNVo> businessVisitsTopN(CockpitParams params) {
		return cockpitMapper.businessVisitsTopN(params);
	}
//

	/**
	 * 查询系统业务健康度TopN
	 *
	 * @param params 首页大屏数据驾驶舱搜索条件
	 * @return 首页大屏-系统业务健康度TopN vo对象
	 */
	@Override
	public List<BusinessHealthyTopNVo> businessHealthyTopN(CockpitParams params) {
		return cockpitMapper.businessHealthyTopN(params);
	}

	/**
	 * 查询系统请求量趋势
	 *
	 * @param params 首页大屏数据驾驶舱搜索条件
	 * @return 首页大屏-系统请求量趋势vo对象
	 */
	@Override
	public RequestTrend requestTrend(CockpitParams params) {
		RequestTrend requestTrend = new RequestTrend();
		List<RequestTrendVo> dayList = new ArrayList<>();
		List<RequestTrendVo> weekList = new ArrayList<>();
		List<RequestTrendVo> monthList = new ArrayList<>();
		List<RequestTrendVo> requestTrendDay = new ArrayList<>();
		List<RequestTrendVo> requestTrendWeek = new ArrayList<>();
		List<RequestTrendVo> requestTrendMonth = new ArrayList<>();

		String startTimeStr = params.getStartTime();
		String endTimeStr = params.getEndTime();
		DateTime parse = YDateUtil.parse(endTimeStr, YDateUtil.YYYY_MM_DD_HH_MM_SS);
		params.setEndTimeStr(YDateUtil.format(parse, YDateUtil.HH_MM_SS));

		// 1、查询今日请求量趋势
		requestTrendDay = cockpitMapper.requestTrend(params, 1);

		// 2、查询过去七天请求量趋势
		String weekStartTime = YDateUtil.getBeforeDayTime(startTimeStr, 7);
		String weekEndTime = YDateUtil.getBeforeDayTime(endTimeStr, 1);
		params.setStartTime(weekStartTime);
		params.setEndTime(weekEndTime);
		requestTrendWeek = cockpitMapper.requestTrend(params, 7);

		// 3、查询过去一个月请求量趋势
		String monthStartTime = YDateUtil.getBeforeDayTime(startTimeStr, 30);
		String monthEndTime = YDateUtil.getBeforeDayTime(endTimeStr, 1);
		params.setStartTime(monthStartTime);
		params.setEndTime(monthEndTime);
		requestTrendMonth = cockpitMapper.requestTrend(params, 30);

		// 4、查询历史最高值
		params.setEndTime(endTimeStr);
		RequestPeak requestPeak = new RequestPeak();
		queryPeakValue(requestPeak, params, "REQUEST");

		// 5. 组装结果集, 进行补0操作
		List<String> timeList = YDateUtil.getTimeListByJDK8(startTimeStr, endTimeStr, YDateUtil.HH_MM, 1);
		Map<String, RequestTrendVo> dayCollectMap = requestTrendDay.stream().collect(Collectors.toMap(RequestTrendVo::getLabelTime, v -> v, (k1, k2) -> k1));
		Map<String, RequestTrendVo> weekCollectMap = requestTrendWeek.stream().collect(Collectors.toMap(RequestTrendVo::getLabelTime, v -> v, (k1, k2) -> k1));
		Map<String, RequestTrendVo> monthCollectMap = requestTrendMonth.stream().collect(Collectors.toMap(RequestTrendVo::getLabelTime, v -> v, (k1, k2) -> k1));
		for (String time : timeList) {
			// 初始化对象，用来补0操作
			RequestTrendVo supplementVo = new RequestTrendVo();
			supplementVo.setLabelTime(time);
			supplementVo.setReqCount(0);
			supplementVo.setAlarmCount(0);
			supplementVo.setFailVisitCount(0);

			RequestTrendVo dayVo = dayCollectMap.get(time);
			RequestTrendVo weekVo = weekCollectMap.get(time);
			RequestTrendVo monthVo = monthCollectMap.get(time);
			dayList.add(ObjectUtil.isNotNull(dayVo) ? dayVo : supplementVo);
			weekList.add(ObjectUtil.isNotNull(weekVo) ? weekVo : supplementVo);
			monthList.add(ObjectUtil.isNotNull(monthVo) ? monthVo : supplementVo);
		}

		requestTrend.setRequestTrendDay(dayList);
		requestTrend.setRequestTrendVoWeek(weekList);
		requestTrend.setRequestTrendVoMonth(monthList);
		requestTrend.setRequestPeak(requestPeak);
		params.setStartTime(startTimeStr);
		params.setEndTime(endTimeStr);

		return requestTrend;
	}


	/**
	 * 查询历史峰值
	 * redis key:
	 * HOME-REQUEST-PEAK-20230602  // 请求量峰值
	 * HOME-FLOW-PEAK-20230602		// 流量峰值
	 *
	 * @param requestPeak 结果对象
	 * @param peakType    峰值类型: 'REQUEST':请求量峰值  'FLOW': 流量峰值
	 * @param params      请求参数
	 */
	public void queryPeakValue(RequestPeak requestPeak, CockpitParams params, String peakType) {

		long startTime = System.currentTimeMillis();
		long peakValue = 0;
		int scale = 1;
		ExecutorService executor = null;
		String peakStartTime = "", scaleType = ScaleTypeEnum.MINUTE.name();

		try {
			String endTime = YDateUtil.format(new Date(), Constants.YYYY_MM_DD_00_00_00);
			List<BaseSearch> timeIntervalList = PartitionUtil.getTimeIntervalList(endTime, 5, 20);
			List<Callable<RequestPeak>> tasks = new ArrayList<>();

			// 查询数据
			for (BaseSearch timeInterval : timeIntervalList) {
				tasks.add(() -> cockpitMapper.queryRequestPeak(params, scale, scaleType, timeInterval, peakType));
			}
			executor = Executors.newFixedThreadPool(tasks.size());
			List<Future<RequestPeak>> futures = executor.invokeAll(tasks);

			// 封装数据
			for (Future<RequestPeak> future : futures) {
				RequestPeak peakVo = future.get();
				if (peakVo != null && peakVo.getRequestCount() > peakValue) {
					peakValue = peakVo.getRequestCount();
					peakStartTime = peakVo.getBeginTime();
				}
			}

			requestPeak.setRequestCount(peakValue);
			requestPeak.setBeginTime(peakStartTime);
			if (StringUtils.isNotEmpty(peakStartTime)) {
				requestPeak.setEndTime(YDateUtil.getAfterMinuteStr(peakStartTime, scale));
			}

			log.info("[BPM模块]-[首页大屏-获取 请求量 历史峰值 信息]-耗时: {} MS", System.currentTimeMillis() - startTime);

		} catch (Exception e) {
			e.printStackTrace();
			log.error("[BPM模块]-[首页大屏-获取 请求量 历史峰值 信息]-报错: {}", e.getMessage());

		} finally {
			if (executor != null) {
				executor.shutdown();
			}
		}

	}

	/**
	 * 查询系统网端告警TopN
	 *
	 * @param params 首页大屏数据驾驶舱搜索条件
	 * @return 首页大屏-系统网端告警TopN vo对象
	 */
	@Override
	public List<SegmentAlarmTopNVo> segmentAlarmTopN(CockpitParams params) {
		// 1、查询系统下的网端
		List<Integer> ids = new ArrayList<>();
		ids.add(params.getSystemId());
		List<ConfSegment> ConfSegments = remoteSegmentService.queryByIds(ids).getData();
		params.setSegmentIdList(ConfSegments.stream().map(ConfSegment::getId).collect(Collectors.toList()));
		// 2、查询系统网端告警TopN
		return cockpitMapper.segmentAlarmTopN(params);
	}

	/**
	 * 查询系统告警趋势
	 *
	 * @param params 首页大屏数据驾驶舱搜索条件
	 * @return 首页大屏-系统告警趋势vo对象
	 */
	@Override
	public SystemAlarmTrend systemAlarmTrend(CockpitParams params) {
		SystemAlarmTrend systemAlarmTrend = new SystemAlarmTrend();
		List<SystemAlarmTrendVo> dayList = new ArrayList<>();
		List<SystemAlarmTrendVo> weekList = new ArrayList<>();
		List<SystemAlarmTrendVo> monthList = new ArrayList<>();

		String startTimeStr = params.getStartTime();
		String endTimeStr = params.getEndTime();
		String endTime = YDateUtil.format(YDateUtil.parse(endTimeStr, YDateUtil.YYYY_MM_DD_HH_MM_SS), YDateUtil.HH_MM_SS);
		if (!"00:00:00".equals(endTime)) {
			params.setEndTimeStr(YDateUtil.format(YDateUtil.parse(endTimeStr, YDateUtil.YYYY_MM_DD_HH_MM_SS), YDateUtil.HH_MM_SS));    // 结束时间字符串
		}

		//1、查询今日系统告警趋势
		List<SystemAlarmTrendVo> systemAlarmTrendDay = cockpitMapper.systemAlarmTrend(params, params.getIndicators(), 1);


		//2、查询过去七天系统告警趋势
		String weekStartTime = YDateUtil.getBeforeDayTime(startTimeStr, 7);
		String weekEndTime = YDateUtil.getBeforeDayTime(endTimeStr, 1);
		params.setStartTime(weekStartTime);
		params.setEndTime(weekEndTime);
		List<SystemAlarmTrendVo> systemAlarmTrendWeek = cockpitMapper.systemAlarmTrend(params, params.getIndicators(), 7);


		//3、查询过去一个月系统告警趋势
		String monthStartTime = YDateUtil.getBeforeDayTime(startTimeStr, 30);
		String monthEndTime = YDateUtil.getBeforeDayTime(endTimeStr, 1);
		params.setStartTime(monthStartTime);
		params.setEndTime(monthEndTime);
		List<SystemAlarmTrendVo> systemAlarmTrendMonth = cockpitMapper.systemAlarmTrend(params, params.getIndicators(), 30);


		// 4. 组装结果集, 进行补0操作
		List<String> timeList = YDateUtil.getTimeListByJDK8(startTimeStr, endTimeStr, YDateUtil.HH_MM, 1);
		Map<String, SystemAlarmTrendVo> dayCollectMap = systemAlarmTrendDay.stream().collect(Collectors.toMap(SystemAlarmTrendVo::getLabelTime, v -> v, (k1, k2) -> k1));
		Map<String, SystemAlarmTrendVo> weekCollectMap = systemAlarmTrendWeek.stream().collect(Collectors.toMap(SystemAlarmTrendVo::getLabelTime, v -> v, (k1, k2) -> k1));
		Map<String, SystemAlarmTrendVo> monthCollectMap = systemAlarmTrendMonth.stream().collect(Collectors.toMap(SystemAlarmTrendVo::getLabelTime, v -> v, (k1, k2) -> k1));
		for (String time : timeList) {
			// 初始化对象，用来补0操作
			SystemAlarmTrendVo supplementVo = new SystemAlarmTrendVo();
			supplementVo.setLabelTime(time);
			supplementVo.setAlarmCount(0);

			SystemAlarmTrendVo dayVo = dayCollectMap.get(time);
			SystemAlarmTrendVo weekVo = weekCollectMap.get(time);
			SystemAlarmTrendVo monthVo = monthCollectMap.get(time);
			dayList.add(ObjectUtil.isNotNull(dayVo) ? dayVo : supplementVo);
			weekList.add(ObjectUtil.isNotNull(weekVo) ? weekVo : supplementVo);
			monthList.add(ObjectUtil.isNotNull(monthVo) ? monthVo : supplementVo);
		}

		systemAlarmTrend.setSystemAlarmTrendDay(dayList);
		systemAlarmTrend.setSystemAlarmTrendWeek(weekList);
		systemAlarmTrend.setSystemAlarmTrendMonth(monthList);

		return systemAlarmTrend;
	}

	@Override
	public List<RequestTrendVo> allTrendList(CockpitParams params) {
		//告警趋势
		List<SystemAlarmTrendVo> alarmList = cockpitMapper.systemAlarmTrend(params, params.getIndicators(), 1);
		Map<String, Integer> alarmMap = new HashMap<>();
		if (alarmList != null && alarmList.size() > 0) {
			alarmMap = alarmList.stream().collect(Collectors.toMap(SystemAlarmTrendVo::getLabelTime, SystemAlarmTrendVo::getAlarmCount, (key1, key2) -> key2));
		}
		//请求趋势
		List<RequestTrendVo> requestList = cockpitMapper.requestTrend(params, 1);
		//访问失败量趋势
		List<FailTrend> failList = cockpitMapper.failTrend(params, 1);
		Map<String, Integer> failMap = new HashMap<>();
		if (failList != null && failList.size() > 0) {
			failMap = failList.stream().collect(Collectors.toMap(FailTrend::getLabelTime, FailTrend::getFailVisitCount, (key1, key2) -> key2));
		}
		if (requestList != null && requestList.size() > 0) {
			for (RequestTrendVo requestVo : requestList) {
				requestVo.setAlarmCount(alarmMap.getOrDefault(requestVo.getLabelTime(), 0));
				requestVo.setFailVisitCount(failMap.getOrDefault(requestVo.getLabelTime(), 0));
			}
		}
		return requestList;
	}

	@Override
	public FlowTrendVo queryFlowTrend(CockpitParams params) {
		FlowTrendVo flowTrendVo = new FlowTrendVo();
		List<FlowTrend> dayList = new ArrayList<>();
		List<FlowTrend> weekList = new ArrayList<>();
		List<FlowTrend> monthList = new ArrayList<>();

		String startTimeStr = params.getStartTime();
		String endTimeStr = params.getEndTime();
		String endTime = YDateUtil.format(YDateUtil.parse(endTimeStr, YDateUtil.YYYY_MM_DD_HH_MM_SS), YDateUtil.HH_MM_SS);
		if (!"00:00:00".equals(endTime)) {
			params.setEndTimeStr(YDateUtil.format(YDateUtil.parse(endTimeStr, YDateUtil.YYYY_MM_DD_HH_MM_SS), YDateUtil.HH_MM_SS));    // 结束时间字符串
		}

		// 1、查询今日系统告警趋势
		List<FlowTrend> flowTrendDay = cockpitMapper.queryFlowTrend(params, 1);

		// 2、查询过去七天系统告警趋势
		String weekStartTime = YDateUtil.getBeforeDayTime(startTimeStr, 7);
		String weekEndTime = YDateUtil.getBeforeDayTime(endTimeStr, 1);
		params.setStartTime(weekStartTime);
		params.setEndTime(weekEndTime);
		List<FlowTrend> flowTrendWeek = cockpitMapper.queryFlowTrend(params, 7);

		// 3、查询过去一个月系统告警趋势
		String monthStartTime = YDateUtil.getBeforeDayTime(startTimeStr, 30);
		String monthEndTime = YDateUtil.getBeforeDayTime(endTimeStr, 1);
		params.setStartTime(monthStartTime);
		params.setEndTime(monthEndTime);
		List<FlowTrend> flowTrendMonth = cockpitMapper.queryFlowTrend(params, 30);

		// 4、查询历史最高值
		params.setEndTime(endTimeStr);
		RequestPeak requestPeak = new RequestPeak();
		queryPeakValue(requestPeak, params, "FLOW");


		// 5. 组装结果集, 进行补0操作
		List<String> timeList = YDateUtil.getTimeListByJDK8(startTimeStr, endTimeStr, YDateUtil.HH_MM, 1);
		Map<String, FlowTrend> dayCollectMap = flowTrendDay.stream().collect(Collectors.toMap(FlowTrend::getLabelTime, v -> v, (k1, k2) -> k1));
		Map<String, FlowTrend> weekCollectMap = flowTrendWeek.stream().collect(Collectors.toMap(FlowTrend::getLabelTime, v -> v, (k1, k2) -> k1));
		Map<String, FlowTrend> monthCollectMap = flowTrendMonth.stream().collect(Collectors.toMap(FlowTrend::getLabelTime, v -> v, (k1, k2) -> k1));
		for (String time : timeList) {
			// 初始化对象，用来补0操作
			FlowTrend supplementVo = new FlowTrend();
			supplementVo.setLabelTime(time);
			supplementVo.setReqCount(0);

			FlowTrend dayVo = dayCollectMap.get(time);
			FlowTrend weekVo = weekCollectMap.get(time);
			FlowTrend monthVo = monthCollectMap.get(time);
			dayList.add(ObjectUtil.isNotNull(dayVo) ? dayVo : supplementVo);
			weekList.add(ObjectUtil.isNotNull(weekVo) ? weekVo : supplementVo);
			monthList.add(ObjectUtil.isNotNull(monthVo) ? monthVo : supplementVo);
		}

		flowTrendVo.setFlowTrendDay(dayList);
		flowTrendVo.setFlowTrendWeek(weekList);
		flowTrendVo.setFlowTrendMonth(monthList);
		flowTrendVo.setRequestPeak(requestPeak);
		params.setStartTime(startTimeStr);
		params.setEndTime(endTimeStr);

		return flowTrendVo;
	}

	@Override
	public BusHandleTrendVo queryBusinessHandleTrend(CockpitParams params) {
		BusHandleTrendVo busHandleTrendVo = new BusHandleTrendVo();
		String startTimeStr = params.getStartTime();
		String endTimeStr = params.getEndTime();
		String endTime = YDateUtil.format(YDateUtil.parse(endTimeStr, YDateUtil.YYYY_MM_DD_HH_MM_SS), YDateUtil.HH_MM_SS);
		if (!"00:00:00".equals(endTime)) {
			params.setEndTimeStr(YDateUtil.format(YDateUtil.parse(endTimeStr, YDateUtil.YYYY_MM_DD_HH_MM_SS), YDateUtil.HH_MM_SS));    // 结束时间字符串
		}

		// 1、查询业务办理时长趋势
		List<BusHandleTrend> trendDay = cockpitMapper.queryBusinessHandleTrend(params, 1);
//		busHandleTrendVo.setBusHandleTrendDay(trendDay);

		// 2、查询过去七天业务办理时长趋势
		String weekStartTime = YDateUtil.getBeforeDayTime(startTimeStr, 7);
		String weekEndTime = YDateUtil.getBeforeDayTime(endTimeStr, 1);
		params.setStartTime(weekStartTime);
		params.setEndTime(weekEndTime);
		List<BusHandleTrend> trendWeek = cockpitMapper.queryBusinessHandleTrend(params, 7);
//		busHandleTrendVo.setBusHandleTrendWeek(trendWeek);

		// 3、查询过去一个月业务办理时长趋势
		String monthStartTime = YDateUtil.getBeforeDayTime(startTimeStr, 30);
		String monthEndTime = YDateUtil.getBeforeDayTime(endTimeStr, 1);
		params.setStartTime(monthStartTime);
		params.setEndTime(monthEndTime);
		List<BusHandleTrend> trendMonth = cockpitMapper.queryBusinessHandleTrend(params, 30);
		// 4. 组装结果集, 进行补0操作
		List<BusHandleTrend> dayList = new ArrayList<>();
		List<BusHandleTrend> weekList = new ArrayList<>();
		List<BusHandleTrend> monthList = new ArrayList<>();
		List<String> timeList = YDateUtil.getTimeListByJDK8(startTimeStr, endTimeStr, YDateUtil.HH_MM, 1);
		Map<String, BusHandleTrend> dayCollectMap = trendDay.stream().collect(Collectors.toMap(BusHandleTrend::getLabelTime, v -> v, (k1, k2) -> k1));
		Map<String, BusHandleTrend> weekCollectMap = trendWeek.stream().collect(Collectors.toMap(BusHandleTrend::getLabelTime, v -> v, (k1, k2) -> k1));
		Map<String, BusHandleTrend> monthCollectMap = trendMonth.stream().collect(Collectors.toMap(BusHandleTrend::getLabelTime, v -> v, (k1, k2) -> k1));
		for (String time : timeList) {
			// 初始化对象，用来补0操作
			BusHandleTrend supplementVo = new BusHandleTrend();
			supplementVo.setLabelTime(time);
			supplementVo.setDealTime(0L);

			BusHandleTrend dayVo = dayCollectMap.get(time);
			BusHandleTrend weekVo = weekCollectMap.get(time);
			BusHandleTrend monthVo = monthCollectMap.get(time);
			dayList.add(ObjectUtil.isNotNull(dayVo) ? dayVo : supplementVo);
			weekList.add(ObjectUtil.isNotNull(weekVo) ? weekVo : supplementVo);
			monthList.add(ObjectUtil.isNotNull(monthVo) ? monthVo : supplementVo);
		}
		busHandleTrendVo.setBusHandleTrendDay(dayList);
		busHandleTrendVo.setBusHandleTrendWeek(weekList);
		busHandleTrendVo.setBusHandleTrendMonth(monthList);

		return busHandleTrendVo;
	}

	@Override
	public List<SegmentReqTopNVo> querySegmentReqTop(CockpitParams params) {

		return cockpitMapper.querySegmentReqTop(params);
	}

	/**
	 * 首页大屏-网端流量top10占比
	 *
	 * @return 首页大屏-网端流量top10占比
	 */
	@Override
	public List<SegmentFlowTopVo> querySegmentFlowTop(CockpitParams params) {
		return cockpitMapper.querySegmentFlowTop(params);
	}

}
